<?php

/**
 * 顺序队列
 */
class SqQueue {
    public $data;
    public $front; //队列头部
    public $rear; //队列尾部
}

function InitSqQueue() {
    $queue = new SqQueue();
    $queue->data = [];
    $queue->front = 0;
    $queue->rear = 0;
    return $queue;
}

/**
 * 入队列
 * @param SqQueue $queue
 * @param $e
 */
function EnSqQueue(SqQueue &$queue, $e) {
    $queue->data[$queue->rear] = $e;
    $queue->rear++; //入队 时 rear++
}

/**
 * 出队列
 * @param SqQueue $queue
 * @return bool
 */
function DeSqQueue(SqQueue &$queue) {
    // 队列为空
    if ($queue->front == $queue->rear) {
        return false;
    }
    $e = $queue->data[$queue->front];
    unset($queue->data[$queue->front]);
    $queue->front++; //出队 时 front++
    return $e;
}

$q = InitSqQueue();
EnSqQueue($q, 'A'); //入队列
EnSqQueue($q, 'B');
EnSqQueue($q, 'C');
print_r($q);
/*
SqQueue Object
(
    [data] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [front] => 0
    [rear] => 3
)
*/

echo DeSqQueue($q), PHP_EOL; //出队列 // A
echo DeSqQueue($q), PHP_EOL; // B
echo DeSqQueue($q), PHP_EOL; // C
print_r($q);
/*
SqQueue Object
(
    [data] => Array
        (
        )

    [front] => 3
    [rear] => 3
)
*/


